package com.hnluchuan.bmjlearning.web.controller.admin;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import com.hnluchuan.bmjlearning.common.ResultCode;
import com.hnluchuan.bmjlearning.common.ServicePackageStatus;
import com.hnluchuan.bmjlearning.common.ServicePackageType;
import com.hnluchuan.bmjlearning.common.UserType;
import com.hnluchuan.bmjlearning.dao.UserDAO;
import com.hnluchuan.bmjlearning.dto.UserDTO;
import com.hnluchuan.utils.exception.BizException;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hnluchuan.bmjlearning.dto.ServicePackageDTO;
import com.hnluchuan.bmjlearning.service.ServicePackageService;

import com.hnluchuan.core.support.Page;
import com.hnluchuan.bmjlearning.web.controller.BaseController;

@Controller
@RequestMapping(value = "/admin/servicePackage", produces="text/html;charset=UTF-8")
public class AdminServicePackageController extends BaseController {

	@Autowired
	private ServicePackageService servicePackageService;

	@RequestMapping(value = "/detail")
	public String detail(Map<String, Object> map, Long id) {
		UserDTO currentUser = getCurrentUserDTO();
		if(currentUser == null || currentUser.getType() != UserType.Manager.getValue()){
			throw new BizException(ResultCode.NotAuthorized.getRemark());
		}

		if (id != null) {
			ServicePackageDTO dto = servicePackageService.load(id);
			map.put("n", dto);
		}
		return "admin/servicePackage/servicePackage_detail";
	}
	
	@ResponseBody
	@RequestMapping(value = "/save")
	public String save(ServicePackageDTO dto) throws Exception {
		UserDTO currentUser = getCurrentUserDTO();
		if(currentUser == null || currentUser.getType() != UserType.Manager.getValue()){
			throw new BizException(ResultCode.NotAuthorized.getRemark());
		}
		if (StringUtils.isBlank(dto.getName())) {
			throw new BizException("请填写账号类型名称");
		}
		if (dto.getId() == null) {
			dto.setStatus(ServicePackageStatus.Enabled.getValue());
			servicePackageService.create(dto);
		} else {
			ServicePackageDTO old = servicePackageService.load(dto.getId());
			old.setStatus(dto.getStatus());
			old.setExpireDays(dto.getExpireDays());
			old.setName(dto.getName());
			old.setType(dto.getType());
			servicePackageService.updateAllFields(old);
		}
		return ok();
	}

	@ResponseBody
	@RequestMapping(value = "/enableOrDisable")
	public String delete(@RequestParam Long id, @RequestParam Integer status) throws IOException {
		UserDTO currentUser = getCurrentUserDTO();
		if(currentUser == null || currentUser.getType() != UserType.Manager.getValue()){
			throw new BizException(ResultCode.NotAuthorized.getRemark());
		}

		int validServicePackageCount = agentServicePackageService.getValidServicePackageCount(null, id).intValue();
		if(status == ServicePackageStatus.Disabled.getValue() && validServicePackageCount > 0){
			throw new BizException(ResultCode.ServicePackageNotUsed.getRemark());
		}
		servicePackageService.enableOrDisableByIds(new Long[]{id}, status);
		return ok();
	}
	
	@RequestMapping(value = "/list")
	public String list(ServicePackageDTO dto, Boolean search, Map<String, Object> map, Page page) {
		UserDTO currentUser = getCurrentUserDTO();
		if(currentUser == null || currentUser.getType() != UserType.Manager.getValue()){
			throw new BizException(ResultCode.NotAuthorized.getRemark());
		}

		List<ServicePackageDTO> dtos = servicePackageService.find(dto, page);
		map.put("list", dtos);
		map.put("query", dto);
		map.put("search", search);
		map.put("page", page);
		map.put("types", ServicePackageType.values());
		return "admin/servicePackage/servicePackage_list";
	}
	
	@RequestMapping(value = "/search")
	public String search(ServicePackageDTO dto, Map<String, Object> map, Page page) {
		UserDTO currentUser = getCurrentUserDTO();
		if(currentUser == null || currentUser.getType() != UserType.Manager.getValue()){
			throw new BizException(ResultCode.NotAuthorized.getRemark());
		}

		List<ServicePackageDTO> dtos = servicePackageService.find(dto, page);
		map.put("list", dtos);
		map.put("query", dto);
		return "admin/servicePackage/servicePackage_search";
	}
	
}
